package org.example.springbootdemo02.service.impl;

import org.example.springbootdemo02.dto.Page;
import org.example.springbootdemo02.mapper.ProductMapper;
import org.example.springbootdemo02.pojo.PageResult;
import org.example.springbootdemo02.pojo.Product;
import org.example.springbootdemo02.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;

    @Override
    public Product getById(Integer id) {
        return productMapper.getById(id);
    }

    @Override
    public List<Product> getByCondition(Map<String, Object> params) {
        Object o = params.get("name");
        System.out.println(o);
        return productMapper.getByCondition(params);
    }

    @Override
    public List<Product> getAll() {
        return productMapper.getAll();
    }

    @Override
    public int insert(Product product) {
        return productMapper.insert(product);
    }

    @Override
    public int batchInsert(List<Product> productList) {
        return productMapper.batchInsert(productList);
    }

    @Override
    public int update(Product product) {
        return productMapper.update(product);
    }

    @Override
    public int dynamicUpdate(Map<String, Object> params) {
        return productMapper.dynamicUpdate(params);
    }

    @Override
    public int delete(Integer id) {
        return productMapper.delete(id);
    }

    @Override
    public int batchDelete(List<Integer> ids) {
        return productMapper.batchDelete(ids);
    }

    @Override
    public int deleteByCondition(Map<String, Object> params) {
        return productMapper.deleteByCondition(params);
    }

    @Override
    public PageResult<Product> getByPage(Page<Product> page) {
        int pageNum = page.getPageNum();
        int pageSize = page.getPageSize();
        int offset = (pageNum - 1) * pageSize;
        page.setTotalPage(offset);
        List<Product> productList = productMapper.getByPage(page);
        int total = productMapper.count(page);
        return new PageResult<>(total, productList);
    }
}
